package com.qst.config;// JdbcConfig.java

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;

/*
等同于<context:property-placeholder location="classpath*:jdbc.properties">
*/
@PropertySource("classpath:jdbc.properties")
public class JdbcConfig {
    /*
    使用纯注解的方式,读取properties文件中的属性,等同于<property name="******" value="${driver}">
     */
    @Value("${db.driver}")
    private String driver;
    @Value("${db.url}")
    private String url;
    @Value("${db.username}")
    private String user;
    @Value("${db.password}")
    private String password;

    /*
    定义dataSource的bean,等同于
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
     */
    @Bean("dataSource")
    public DataSource getDataSource(){
        // 等用于set属性注入<property name="driverClassName" value="driver">
        DruidDataSource ds = new DruidDataSource();
        ds.setDriverClassName(driver);
        ds.setUrl(url);
        ds.setUsername(user);
        ds.setPassword(password);
        return ds;
    }
}